//
//  MDHRouter.m
//  MDHPT
//
//  Created by 高得华 on 2019/4/27.
//  Copyright © 2019年 高得华. All rights reserved.
//

#import "MDHRouter.h"
#import <pthread/pthread.h>
#import "MDHMacroHeader.h"

@interface MDHRouter ()

/// viewModel到viewController的映射
@property (nonatomic, copy) NSDictionary *viewModelViewMappings;
/// APP的网络服务层
@property (nonatomic, readwrite, strong) MDHViewModelServicesImpl * services;
/// APP管理的导航栏的堆栈
@property (nonatomic, readwrite, strong) MDHNavigationControllerStack *navigationControllerStack;

///


@end

@implementation MDHRouter

static MDHRouter *sharedInstance_ = nil;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [super allocWithZone:zone];
    });
    return sharedInstance_;
}

- (id)copyWithZone:(NSZone *)zone
{
    return sharedInstance_;
}

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [[self alloc] init];
    });
    return sharedInstance_;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        // Config Service
        self.services = [[MDHViewModelServicesImpl alloc] init];
        // Config Nav Stack
        self.navigationControllerStack = [[MDHNavigationControllerStack alloc] initWithServices:self.services];
        self.viewModelViewMappings = @{};
    }
    return self;
}

- (MDHViewController *)viewControllerForViewModel:(MDHViewModel *)viewModel {
    NSString * cbn = [[[NSBundle mainBundle] infoDictionary][@"CFBundleName"] stringByAppendingString:@"."];
    NSString * vm = NSStringFromClass(viewModel.class);
    if ([vm hasPrefix:cbn]) {
        vm = [vm substringFromIndex:cbn.length];
    }
    NSString *viewController = @"";
    if (self.viewModelViewMappings.count > 0) {
        viewController = self.viewModelViewMappings[vm];
    }
    if (viewController.length <= 0) {
        DTLog(@"- ==%@对应的viewController:为空",vm);
        return [MDHViewController new];
    }
    
    Class class = NSClassFromString(viewController);
    if (class == nil) {
        class = NSClassFromString([cbn stringByAppendingString:viewController]);
    }
    
    NSParameterAssert([class isSubclassOfClass:[MDHViewController class]]);
    NSParameterAssert([class instancesRespondToSelector:@selector(initWithViewModel:)]);
    //[self studayDxc];
    MDHViewController * vc = [[class alloc] initWithViewModel:viewModel];
    viewModel.view = vc.view;
    return vc;
}


/// 这里是viewModel -> ViewController的映射
/// If You Use Push 、 Present 、 ResetRootViewController ,You Must Config This Dict
- (void)viewModelViewMappings:(NSDictionary *)dict {
    NSMutableDictionary * dict1 = @{
    /// - ==== Tabbar H5 其他 ===== -
    @"MDHWebViewModel":@"MDHWKWebViewController",///H5页面
    @"MDHSegmentedControlViewModel":@"MDHHtTitleViewController",//Segment
    @"MDHEditTextViewModel": @"MDHEditTextViewController",///输入文本视图
    }.mutableCopy;
    if (dict) {
        [dict1 addEntriesFromDictionary:dict];
    }
    self.viewModelViewMappings = dict1.copy;
}

/*
/// 这里是viewModel -> ViewController的映射
/// If You Use Push 、 Present 、 ResetRootViewController ,You Must Config This Dict
- (NSDictionary *)viewModelViewMappings {
    return @{

        /// - ==== Tabbar H5 其他 ===== -
        @"MDHWebViewModel":@"MDHWKWebViewController",///H5页面
        @"MDHSegmentedControlViewModel":@"MDHHtTitleViewController",//Segment
        @"MDHEditTextViewModel": @"MDHEditTextViewController",///输入文本视图
        
        ///橙子语音
        @"MDHOvHTabbarViewModel":@"MDHOvHTabbarViewController",//Tabbar
        /// - ====== 四大模块首页
        @"MDHOvHomeViewModel": @"MDHOvHomeViewController",///首页
        @"MDHOvRankViewModel":@"MDHOvRankViewController",//排行榜
        @"MDHOvMessageListViewModel":@"MDHOvMessageListViewController",//消息
        @"MDHOvMineViewModel":@"MDHOvMineViewController",//我的
        ///首页
        @"MDHOvHomeListViewModel":@"MDHOvHomeListViewController",///首页中的子界面
        ///排行榜
        @"MDHOvRankListViewModel":@"MDHOvRankListViewController",///排行榜中的子界面
        ///登录注册
        @"MDHOvLrFristViewModel":@"MDHOvLrFristViewController",///登录第一步
    
        ///吱音交友
        /// - ====== 四大模块首页
        @"MDHHomeViewModel": @"MDHHomeViewController",///首页
        @"MDHFindViewModel":@"MDHFindViewController",//发现
        @"MDHMessageViewModel":@"MDHMessageViewController",//消息
        @"MDHMineViewModel":@"MDHMineViewController",//我的
        /// Tabbar
        @"MDHHomePageViewModel":@"MDHHomePageViewController",///TabBar
         /// - =====  登录注册 ====== -
         @"MDHLoginViewModel":@"MDHLoginViewController",///登录界面
         @"MDHVerificationCodeViewModel":@"MDHVerificationCodeViewController",///验证码
         @"MDHForgetPasswordViewModel":@"MDHForgetPasswordViewController",///忘记密码
         @"MDHResetPasswordViewModel":@"MDHResetPasswordViewController",///重置密码
         @"MDHBasicUserInforViewModel":@"MDHBasicUserInforViewController",///注册成功之后 - 设置基本的信息
         /// - ===== 我的界面 =====
         @"MDHSetViewModel":@"MDHSetViewController",///设置界面
         @"MDHRecentVisitorsViewModel":@"MDHRecentVisitorsViewController",///最近访客
         @"MDHFollowFansViewModel":@"MDHFollowFansViewController",///关注 粉丝
         @"MDHFollowFansChildViewModel":@"MDHFollowFansChildViewController",///关注 粉丝 的内容视图
         @"MDHPersonalHomeViewModel":@"MDHPersonalHomeViewController",///个人主页
         @"MDHPHomeChildMeansViewModel":@"MDHPHomeChildMeansViewController",///个人主页 - 资料
         @"MDHPHomeChildAlbumViewModel":@"MDHPHomeChildAlbumViewController",///个人主页 - 相册
         @"MDHPersonalEditViewModel":@"MDHPersonalEditViewController",///编辑界面
         @"MDHVoiceIntroductionViewModel":@"MDHVoiceIntroductionViewController",///语音介绍
         @"MDHInterestViewModel":@"MDHInterestViewController",///兴趣爱好
         @"MDHSchoolViewModel":@"MDHSchoolViewController",///学校
         @"MDHProfessionViewModel":@"MDHProfessionViewController",///职业
         @"MDHBindAccountViewModel":@"MDHBindAccountViewController",///绑定界面
         @"MDHRenewalMobileViewModel": @"MDHRenewalMobileViewController",///更改换绑手机号
         @"MDHBindMobileViewModel": @"MDHBindMobileViewController",///更改换绑手机号
         @"MDHMessageReminderViewModel":@"MDHMessageReminderViewController",///消息提醒设置
         @"MDHIDAuthenViewModel":@"MDHIDAuthenViewController",///实名认证
         @"MDHMineWalletViewModel":@"MDHMineWalletViewController",///我的钱包
         @"MDHMRechargeViewModel":@"MDHMRechargeViewController",///充值
         @"MDHMwrDetailViewModel":@"MDHMwrDetailViewController",///充值明细
         @"MDHMwrSeDetailViewModel":@"MDHMwrSeDetailViewController",///充值明细主视图
         /// - ===== 首页 ===== -
         @"MDHPublishDynamicsViewModel":@"MDHPublishDynamicsViewController",//发布动态
         @"MDHHRecommendViewModel":@"MDHHRecommendViewController",///推荐动态
         @"MDHHOtherDynamicViewModel":@"MDHHOtherDynamicViewController",///关注、附近动态
         @"MDHAddressViewModel":@"MDHAddressViewController",///所在位置
         @"MDHDynamicsDetailViewModel":@"MDHDynamicsDetailViewController",///动态详情(纯文字、图片)
         @"MDHDouYinViewModel":@"MDHDouYinViewController",///动态详情(视频)
         @"MDHHReplyDetailViewModel":@"MDHHReplyDetailViewController",///某条评论的详情
         @"MDHHDynamicLikesViewModel":@"MDHHDynamicLikesViewController",///点赞的人员列表
         ///--- 消息 ----
         @"MDHChatViewModel":@"MDHChatViewController",///聊天
         @"MDHInteractiveNewsViewModel":@"MDHInteractiveNewsViewController",///互动消息
         @"MDHOfficialAnnotationViewModel":@"MDHOfficialAnnotationViewController",///官方通知
         @"MHLocationViewModel":@"MHLocationViewController",
         @"MHGenderViewModel":@"MHGenderViewController",
         @"MHPlugViewModel":@"MHPlugViewController",
         @"MHPlugDetailViewModel":@"MHPlugDetailViewController",
         @"MHAccountSecurityViewModel":@"MHAccountSecurityViewController",
         @"MHTestViewModel":@"MHTestViewController",
         @"MHNotificationViewModel":@"MHNotificationViewController",
         @"MHFreeInterruptionViewModel":@"MHFreeInterruptionViewController",
         @"MHAboutUsViewModel":@"MHAboutUsViewController",
         @"MHPrivacyViewModel":@"MHPrivacyViewController",
         @"MHGeneralViewModel":@"MHGeneralViewController",
         @"MHEmotionViewModel":@"MHEmotionViewController",
         @"MHMomentViewModel":@"MHMomentViewController",
         @"MHProfileInfoViewModel":@"MHProfileInfoViewController",
         ///---- 发现 、聊天室 =====
         @"MDHChatroomCreateViewModel":@"MDHChatroomCreateViewController",///创建聊天室
         @"MDHChatroomViewModel":@"MDHChatroomViewController",///聊天室主界面
         @"MDHChatroomAdminViewModel":@"MDHChatroomAdminViewController",///管理聊天室
         @"MDHFindChildViewModel":@"MDHFindChildViewController",///聊天室 列表界面
         @"MDHChatroomOnlineViewModel":@"MDHChatroomOnlineViewController",///在线人员
         ///---- 歌曲 -----
         @"MDHMusicListViewModel":@"MDHMusicListViewController",///音乐列表
         @"MDHMusicSearchViewModel":@"MDHMusicSearchViewController",///搜索歌曲
         };
    
}
*/

///多线程的学习
- (void)studayDxc {
    ///学习新函数或者方法的三要素
    ///1.函数的功能
    ///2.函数的参数
    ///3.函数的返回值
    //MARK: pthread
    ///创建多线程 C语言中的
    ///第一个参数 线程编号的指针
    ///第二个参数 线程的属性 nil(oc中的空对象) NULL C语言空指针
    ///第三个参数 void * (*) (void *) void *指向任何类型的指针 类似于OC中的 id
    ///第四个参数 要执行函数的参数
    
    ///函数的返回值 int 0代表成功 1代表失败
    pthread_t pthread;//线程编号
    int result = pthread_create(&pthread, NULL, demo, NULL);
    if (result == 0) {
        NSLog(@"线程创建成功");
    }else{
        NSLog(@"线程创建失败");
    }
    
    
    //MARK: - == NSThread
    ///1.无参数
    ///方法一:
    NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(demoNSThread1) object:nil];
    [thread start];
    ///方法二:
    [NSThread detachNewThreadSelector:@selector(demoNSThread1) toTarget:self withObject:nil];
    ///方法三:
    [self performSelectorInBackground:@selector(demoNSThread1) withObject:nil];
    
    ///带参数
    ///方法四
    NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(demoNSThread2:) object:@"参数"];
    [thread1 start];
}

///函数
void *demo(void *param) {
    NSLog(@"测试线程pthread, %@", [NSThread currentThread]);
    return NULL;
}

///demo1 NSThread
- (void)demoNSThread1 {
    NSLog(@"当前线程:%@",[NSThread currentThread]);
}

- (void)demoNSThread2:(NSString *)param {
    NSLog(@"当前线程:%@, 参数:%@",[NSThread currentThread], param);
}

@end
